// 生产工序 - 商品

<template>
  <view>
    <mes-form :mesForm="mesForm"></mes-form>
    <mes-button type="3" :form="mesButtonForm"></mes-button>
  </view>
</template>

<script>
export default {
  async onLoad(options) {
    this.index = options.index
    this.mesForm.row = JSON.parse(
      JSON.stringify(getApp().globalData.goods.list[this.index])
    )
    // 获取商品单位
    this.mesForm.items[3].slots.select.options = await this.$tools.getUnit(
      this.mesForm.row.goods_id || this.mesForm.row.id
    )
  },
  data() {
    return {
      index: '',
      mesForm: {
        row: {},
        items: [
          {
            title: '商品编号',
            slots: {
              field: 'goods_code'
            }
          },
          {
            title: '商品名称',
            slots: {
              field: 'goods_name'
            }
          },
          {
            title: '商品备注',
            slots: {
              field: 'goods_note'
            }
          },
          {
            title: '商品单位',
            slots: {
              field: 'unit_name',
              type: 'select',
              icon: 'arrow',
              select: {
                placeholder: '请输入关键词',
                options: [],
                changeEvent: (item) => {
                  this.mesForm.row.goods_unit = item.id
                  this.mesForm.row.unit_name = item.name
                  this.formatInfo('goods_unit')
                }
              }
            }
          },
          {
            title: '数量',
            slots: {
              field: 'number',
              type: 'input',
              input_type: 'digit',
              inputEvent: () => {
                this.formatInfo('number')
              }
            }
          },
          {
            title: '基本单位',
            slots: {
              field: 'default_unit_name'
            }
          },
          {
            title: '基本数量',
            slots: {
              field: 'basic_nmber'
            }
          },
          {
            title: '单价',
            slots: {
              field: 'purchase',
              type: 'input',
              input_type: 'digit',
              inputEvent: () => {
                this.formatInfo('unit_price')
              }
            }
          },
          {
            title: '金额',
            slots: {
              field: 'money',
              type: 'input',
              input_type: 'digit',
              inputEvent: () => {
                this.formatInfo('all_money')
              }
            }
          }
        ]
      },
      mesButtonForm: {
        leftText: '删除',
        rightText: '保存',
        leftEvent: () => {
          uni.showModal({
            title: '提示',
            content: '是否确认删除？',
            success: ({ confirm }) => {
              if (confirm) {
                getApp().globalData.goods.list.splice(this.index, 1)
                this.$tools.successBack('删除成功')
              }
            }
          })
        },
        rightEvent: () => {
          getApp().globalData.goods.list[this.index] = this.mesForm.row
          this.$tools.successBack('保存成功')
        }
      }
    }
  },
  methods: {
    // 获取变动价格
    async formatInfo(now_field) {
      const postForm = {
        // 商品编号
        goods_code: this.mesForm.row.goods_code,
        // 商品属性ID（无属性请传空）
        goods_attr: '',
        // 商品单位ID
        goods_unit: this.mesForm.row.goods_unit || this.mesForm.row.unit_id,
        // 仓库ID
        store: '',
        // 数量
        number: this.mesForm.row.number,
        // 商品单价
        unit_price: this.mesForm.row.purchase,
        // 商品含税单价
        tax_included: this.mesForm.row.purchase,
        // 折扣率
        discount: '',
        // 折扣额
        dis_money: '',
        // 购货金额
        all_money: this.mesForm.row.money,
        // 税率
        tax_rate: 0,
        // 税额
        tax_money: '',
        // 价税合计
        money_tax_price: this.mesForm.row.money,
        // 当前变动字段
        now_field
      }
      const res = await this.$api.buy.common.formatInfo(postForm)
      if (res.code == 200) {
        this.mesForm.row.number = res.data.number
        this.mesForm.row.purchase = res.data.unit_price
        this.mesForm.row.money = res.data.all_money
        this.mesForm.row.basic_nmber = res.data.basic_nmber
      }
    }
  }
}
</script>
